//
//  PersonalUserPhotoVideoCell.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalUserPhotoVideoCell.h"
#import <AVFoundation/AVFoundation.h>

@interface PersonalUserPhotoVideoCell ()

@property (nonatomic, strong) UILabel *HOTCHATtimeLab;
@property (nonatomic, strong) UIImageView *HOTCHATlvImgView;
@property (nonatomic, strong) UIImageView *HOTCHATbackImgView;
@property (nonatomic, strong) UIImageView *HOTCHATroundImageView;
@property (nonatomic, strong) UIImageView *HOTCHATshadowBackImgView;
@property (nonatomic, strong) UIView *HOTCHATshadowMsgView;
@property (nonatomic, strong) UILabel *HOTCHATshadowMsgLab;
@end

@implementation PersonalUserPhotoVideoCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    self.clipsToBounds = YES;
    self.layer.cornerRadius = 5;
    
    self.HOTCHATphotoImageView = [[UIImageView alloc] init];
    self.HOTCHATphotoImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:self.HOTCHATphotoImageView];
    
    self.HOTCHATlvImgView = [[UIImageView alloc] init];
    self.HOTCHATlvImgView.image = [UIImage imageNamed:@"viodeo_time_bkg"];
    self.HOTCHATlvImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.HOTCHATlvImgView.clipsToBounds = YES;
    [self addSubview:self.HOTCHATlvImgView];
    
    self.HOTCHATtimeLab = [[UILabel alloc] init];
    self.HOTCHATtimeLab.font = [UIFont systemFontOfSize:12];
    self.HOTCHATtimeLab.textColor = [UIColor whiteColor];
    self.HOTCHATtimeLab.text = @"1:00";
    [self addSubview:self.HOTCHATtimeLab];
    
    self.HOTCHATshadowBackImgView = [[UIImageView alloc] init];
    self.HOTCHATshadowBackImgView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:self.HOTCHATshadowBackImgView];
    
    self.HOTCHATshadowMsgView = [[UIView alloc] init];
    self.HOTCHATshadowMsgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [self addSubview:self.HOTCHATshadowMsgView];
    
    self.HOTCHATshadowMsgLab = [[UILabel alloc] init];
    self.HOTCHATshadowMsgLab.font = [UIFont systemFontOfSize:12];
    self.HOTCHATshadowMsgLab.textColor = [UIColor whiteColor];
    self.HOTCHATshadowMsgLab.numberOfLines = 2;
    self.HOTCHATshadowMsgLab.textAlignment = NSTextAlignmentCenter;
    self.HOTCHATshadowMsgLab.adjustsFontSizeToFitWidth = YES;
    self.HOTCHATshadowMsgLab.minimumScaleFactor = 0.6;
    [self addSubview:self.HOTCHATshadowMsgLab];
    
    self.HOTCHATbackImgView = [[UIImageView alloc] init];
    self.HOTCHATbackImgView.image = [UIImage imageNamed:@"photo_selfback_imag"];
    self.HOTCHATbackImgView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:self.HOTCHATbackImgView];
    
    self.HOTCHATroundImageView = [[UIImageView alloc] init];
    self.HOTCHATroundImageView.image = [UIImage imageNamed:@"photo_selnormall"];
    [self addSubview:self.HOTCHATroundImageView];
    
    @weakify(self);
    [RACObserve(self, model.isSelected) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if (self.model.isSelected) {
            self.HOTCHATroundImageView.image = [UIImage imageNamed:@"photo_del_select"];
        }else{
            self.HOTCHATroundImageView.image = [UIImage imageNamed:@"photo_selnormall"];
        }
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.HOTCHATphotoImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.HOTCHATtimeLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.height.mas_equalTo(25);
        make.bottom.equalTo(self);
    }];
    [self.HOTCHATlvImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.height.mas_equalTo(25);
    }];
    [self.HOTCHATbackImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.HOTCHATroundImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(20);
        make.right.equalTo(self).offset(-9);
        make.top.equalTo(self).offset(9);
    }];
    [self.HOTCHATshadowBackImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.HOTCHATshadowMsgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.HOTCHATshadowMsgLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.lessThanOrEqualTo(self).offset(-5);
    }];
}

- (void)setModel:(PersonalUserPhotoModel *)model{
    _model = model;
    
    [self HOTCHATthumbnailImageForVideo:model.videoAddress];
    NSInteger times = model.time.integerValue;
    self.HOTCHATtimeLab.text = [NSString stringWithFormat:@"%zd:%02zd",times / 60,times % 60];
    
    self.HOTCHATshadowBackImgView.image = [UIImage imageNamed:model.HOTCHATstatusBack];
    NSString *shmsgstr = model.HOTCHATstatusMsg;
    if (shmsgstr.length > 0) {
        self.HOTCHATshadowMsgView.hidden = NO;
        self.HOTCHATshadowMsgLab.hidden = NO;
        self.HOTCHATshadowMsgLab.text = shmsgstr;
    }else{
        self.HOTCHATshadowMsgView.hidden = YES;
        self.HOTCHATshadowMsgLab.hidden = YES;
    }
}

- (void)setIsEditMode:(BOOL)isEditMode{
    _isEditMode = isEditMode;
    if (isEditMode) {
        self.HOTCHATbackImgView.hidden = NO;
        self.HOTCHATroundImageView.hidden = NO;
    }else{
        self.HOTCHATbackImgView.hidden = YES;
        self.HOTCHATroundImageView.hidden = YES;
    }
}

- (void)HOTCHATthumbnailImageForVideo:(NSString *)vurl {
    self.HOTCHATphotoImageView.image = [UIImage imageNamed:@"photo_placeholder"];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *videoURL = [NSURL URLWithString:[vurl HOTCHATTotalStringUrl]];
        
        YYImageCache *chches = [YYImageCache sharedCache];
        UIImage *thumbImg = [chches getImageForKey:videoURL.absoluteString];
        
        if (thumbImg == nil) {
            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
            AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
            gen.appliesPreferredTrackTransform = YES;
            CMTime time = CMTimeMakeWithSeconds(1.0, 600);
            NSError *error = nil;
            CMTime actualTime;
            CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
            thumbImg = [[UIImage alloc] initWithCGImage:image];

            [chches setImage:thumbImg forKey:videoURL.absoluteString];

            dispatch_async(dispatch_get_main_queue(), ^{
                if ([vurl isEqualToString:self.model.videoAddress]) {
                    if (thumbImg) {
                        self.HOTCHATphotoImageView.image = thumbImg;
                    }
                }
            });
        }else {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([vurl isEqualToString:self.model.videoAddress]) {
                    self.HOTCHATphotoImageView.image = thumbImg;
                }
            });
        }
    });
    
}
@end
